package com.example.mvvmbase;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.mvvmbase.databinding.ActivityMainBinding;
import com.example.mvvmbase.fragment.MainFragment;
import com.example.mvvmbase.viewmodel.MainViewModel;
import com.example.mvvmbase.wedgit.TabLayout;
import com.mobian.mvvm.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {


    private List<Fragment> mFragments;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return 0;
    }


    @Override
    public boolean useToolbar() {
        return false;
    }

    @Override
    public void initData() {
        super.initData();

        binding.setViewModel(new MainViewModel(getApplication()));
        //初始化Fragment
        initFragment();
        //初始化底部Button
        initBottomTab();
    }

    @Override
    public String setTitle() {
        return null;
    }


    /**
     * 替换项目图标即可
     */
    private void initBottomTab() {
        com.example.mvvmbase.wedgit.TabLayout tabLayout = binding.pagerBottomTab;

        tabLayout.addTabItem(newTabItem(R.mipmap.ic_tab_item_discovery_normal, R.mipmap.ic_tab_item_discovery_checked, getResources().getString(R.string.tab_item_discovery)));
        tabLayout.addTabItem(newTabItem(R.mipmap.ic_tab_item_task_normal, R.mipmap.ic_tab_item_task_checked, getResources().getString(R.string.tab_item_task)));
        tabLayout.addTabItem(newTabItem(R.mipmap.ic_tab_item_video_normal, R.mipmap.ic_tab_item_video_checked, null));
        tabLayout.addTabItem(newTabItem(R.mipmap.ic_tab_item_moment_normal, R.mipmap.ic_tab_item_moment_checked, getResources().getString(R.string.tab_item_moment)));
        tabLayout.addTabItem(newTabItem(R.mipmap.ic_tab_item_mine_normal, R.mipmap.ic_tab_item_mine_checked, getResources().getString(R.string.tab_item_mine)));
        tabLayout.setSelectItem(0);
        tabLayout.setOnTabSelectListener(new TabLayout.OnTabSelectListener() {
            @Override
            public void onTabSelected(View v, int position) {
                commitAllowingStateLoss(position);
            }
        });
    }


    private com.example.mvvmbase.wedgit.TabLayout.TabItem newTabItem(int drawable, int selectDrawable, String text) {
        com.example.mvvmbase.wedgit.TabLayout.TabItem item = new com.example.mvvmbase.wedgit.TabLayout.TabItem();
        item.setDefaultDrawable(drawable);
        item.setSelectedDrawable(selectDrawable);
        item.setText(text);
        item.setTextDefaultColor(getResources().getColor(R.color.tab_item_color_normal));
        item.setTextCheckedColor(getResources().getColor(R.color.tab_item_color_selected));
        return item;
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(new MainFragment(0));
        mFragments.add(new MainFragment(1));
        mFragments.add(new MainFragment(2));
        mFragments.add(new MainFragment(3));
        mFragments.add(new MainFragment(4));
        commitAllowingStateLoss(0);
    }

    public void commitAllowingStateLoss(int position) {
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(position + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragments.get(position);
            transaction.add(R.id.frameLayout, currentFragment, position + "");
        }
        transaction.commitAllowingStateLoss();
    }

    //隐藏所有Fragment
    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragments.size(); i++) {
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(i + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }
}